package com.jrh.web.ajax.pub;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.product.ProductService;
import com.jrh.biz.product.ref.ProductRefService;
import com.jrh.common.json.JacksonHelper;
import com.jrh.domain.entity.Product;

/**
 * 产品查询ajax
 * 
 * @author jasen.hsu.cn@gmail.com 2012-8-13 下午9:32:08
 */
@Controller
@RequestMapping("/product_ajax")
public class ProductSearchAjax {

    @Autowired
    private ProductService    productService;

    @Autowired
    private ProductRefService productRefService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/plist")
    public String listProductByCondition(Long brandId, String name, HttpServletRequest request,
                                         HttpServletResponse response) {
        try {
            List<Product> productList = productService.listProductByCondition(brandId, name);
            return JacksonHelper.toJSON(productList);
        } catch (Exception e) {
            return "";
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/pr/plist")
    public String listProductByProductRef(Long refId, String refType, HttpServletRequest request,
                                          HttpServletResponse response) {
        try {
            List<Product> productList = productService.listProductByProductRef(refId, refType);
            return JacksonHelper.toJSON(productList);
        } catch (Exception e) {
            return "";
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/pr/update")
    public String updateProductRef(Long refId, String refType, String[] productIds, HttpServletRequest request,
                                   HttpServletResponse response) {
        try {
            productRefService.updateProductRef(refId, refType, productIds);
            return Boolean.TRUE.toString();
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }
}
